package com.mugui.base.client.net.bagsend;


import com.mugui.base.base.Autowired;
import com.mugui.base.base.Component;
import com.mugui.base.client.net.baghandle.EnhancedWebSocketClient;
import com.mugui.base.client.net.baghandle.NetHandle;
import com.mugui.base.client.net.classutil.DataSave;
import lombok.Getter;
import lombok.Setter;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;

@Component
public class WsHandle {

    @Autowired
    private NetHandle handle;


    @Getter
    private ConcurrentHashMap<String, SendQueue> clientMap = new ConcurrentHashMap<>();

    public SendQueue getClient(String serverUrl) {
        return clientMap.get(serverUrl);
    }

    @Setter
    private int send_queue_size = 10;

    public void init(String serverUrl) {
        SendQueue client = getClient(serverUrl);
        if (client == null) {
            clientMap.put(serverUrl, client = new SendQueue(serverUrl));
        }
    }


    private EnhancedWebSocketClient initWS(String serverUrl) {
        if (serverUrl.startsWith("http")) {
            serverUrl = serverUrl.replace("http", "ws");
        }
        return new EnhancedWebSocketClient(serverUrl, 3);
    }

    /**
     * 毫秒：默认直接过期
     */
    @Getter
    @Setter
    private static long expire_time = 0;

    public static class SendQueue extends ConcurrentLinkedQueue<EnhancedWebSocketClient> {

        @Getter
        private String serverUrl;


        private WsHandle wsHandle;

        public SendQueue(String serverUrl) {
            this.serverUrl = serverUrl;
            wsHandle = DataSave.context.getBean(WsHandle.class);
        }

        public void sendMessage(String string) {
            EnhancedWebSocketClient poll = poll();
            if (poll == null) {
                poll = wsHandle.initWS(serverUrl);
                System.out.println("无可用，初始化链接-> " + serverUrl + " " + poll);
            }
            System.out.println("发送->" + serverUrl + " " + poll.isOpen());
            poll.sendMessage(this, string);

        }

        public void sendPongMessage(String id, String data) {
            EnhancedWebSocketClient poll = poll();
            if (poll == null) {
                poll = wsHandle.initWS(serverUrl);
                System.out.println("无可用，初始化链接-> " + serverUrl + " " + poll);
            }
            System.out.println("发送->" + serverUrl + " " + poll.isOpen());
            poll.sendPongMessage(this, id, data);
        }

        @Override
        public synchronized EnhancedWebSocketClient poll() {
            EnhancedWebSocketClient first = null;
            EnhancedWebSocketClient poll = super.poll();
            while (true) {
                if (poll == null) return null;
                if (poll.getExpire_time() != -1) {
                    if (System.currentTimeMillis() - poll.getExpire_time() > 0) {
                        poll.close();
                        poll = super.poll();
                        continue;
                    }
                    if (first == null) {
                        first = poll;
                    } else if (first == poll) {
                        super.add(poll);
                        return null;
                    }
                    super.add(poll);
                    poll = super.poll();
                    continue;
                }
                return poll;
            }
        }

        @Override
        public boolean add(EnhancedWebSocketClient enhancedWebSocketClient) {
            if (enhancedWebSocketClient.getExpire_time() <= 0 && System.currentTimeMillis() - enhancedWebSocketClient.getInit_time() >= expire_time) {
                System.out.println("过期:" + enhancedWebSocketClient);
                //标记为过期
                enhancedWebSocketClient.expire();
            }
            return super.add(enhancedWebSocketClient);
        }


    }


}
